<?php

namespace common\models\c2\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\c2\entity\Community;

/**
 * CommunitySearch represents the model behind the search form about `common\models\c2\entity\Community`.
 */
class CommunitySearch extends Community
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status', 'cc_room_sum', 'unit_sum', 'wall_psum', 'umber_start', 'umber_end', 'units', 'total_floor', 'th_per_floor'], 'integer'],
            [['name', 'area', 'address', 'umber_unit', 'remarks', 'user_name', 'password', 'contact', 'phone'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Community::find();

        if (!Yii::$app->user->can('P_SYSTEM')) {
            $this->id2 = Yii::$app->user->getCommunityID();
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'sortParam' => $this->getSortParamName(),
            ],
            'pagination' => [
                'pageParam' => $this->getPageParamName(),
                'pageSize' => 20,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
            'cc_room_sum' => $this->cc_room_sum,
            'unit_sum' => $this->unit_sum,
            'wall_psum' => $this->wall_psum,
            'umber_start' => $this->umber_start,
            'umber_end' => $this->umber_end,
            'units' => $this->units,
            'total_floor' => $this->total_floor,
            'th_per_floor' => $this->th_per_floor,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'area', $this->area])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'umber_unit', $this->umber_unit])
            ->andFilterWhere(['like', 'remarks', $this->remarks])
            ->andFilterWhere(['like', 'user_name', $this->user_name])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'phone', $this->phone]);

        return $dataProvider;
    }
    
    public function getPageParamName($splitor = '-'){
        $name = "CommunityPage";
        return \Yii::$app->czaHelper->naming->toSplit($name);
    }
    
    public function getSortParamName($splitor = '-'){
        $name = "CommunitySort";
        return \Yii::$app->czaHelper->naming->toSplit($name);
    }
}
